package net.xdclass.service.impl;

import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.manager.LinkGroupManager;
import net.xdclass.model.LinkGroupDO;
import net.xdclass.req.LinkGroupAddReq;
import net.xdclass.req.LinkGroupUpdateReq;
import net.xdclass.service.LinkGroupService;
import net.xdclass.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class LinkGroupServiceImpl implements LinkGroupService {

    @Autowired
    private LinkGroupManager linkGroupManager;

    /**
     * 添加分组
     * @param req
     * @return
     */
    @Override
    public int add(LinkGroupAddReq req) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setAccountNo(accountNo);
        linkGroupDO.setTitle(req.getTitle());
        return linkGroupManager.add(linkGroupDO);
    }

    /**
     * 删除分组
     * @param groupId
     * @return
     */
    @Override
    public int del(Long groupId) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        return linkGroupManager.del(groupId, accountNo);
    }

    /**
     * 修改组名
     * @param req
     * @return
     */
    @Override
    public int update(LinkGroupUpdateReq req) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(req.getTitle());
        linkGroupDO.setId(req.getId());
        linkGroupDO.setAccountNo(accountNo);
        return linkGroupManager.update(linkGroupDO);
    }

    /**
     * 根据ID查询组详细信息
     * @param groupId
     * @return
     */
    @Override
    public LinkGroupVO detail(Long groupId) {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId, accountNo);
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        BeanUtils.copyProperties(linkGroupDO, linkGroupVO);
        return linkGroupVO;
    }

    /**
     * 查询用户下的所有分组
     * @return
     */
    @Override
    public List<LinkGroupVO> listAllGroup() {
        long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupDO> linkGroupDOList = linkGroupManager.listAllGroup(accountNo);

        List<LinkGroupVO> linkGroupVOList = linkGroupDOList.stream().map(item -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(item, linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());

        return linkGroupVOList;
    }

}
